!***********************************************************************
!                                                                       
!                         **************                                    
!                         *            *                                    
!                         *   PyEgs5   *                                    
!                         *            *                                    
!                         **************                                    
!                                                                       
!               An EGS5 user code for Python binding. 
!***********************************************************************
!23456789|123456789|123456789|123456789|123456789|123456789|123456789|12

      SUBROUTINE SHOWER(iqin,ein,xin,yin,zin,uin,vin,win,irin,wtin,ISUB,
     *ISUB2)
      
      implicit none
                 
c2fpy intent(callback) ISUB,ISUB2
      EXTERNAL ISUB,ISUB2
      integer ISUB,ISUB2
cf2py integer y,x
cf2py y=ISUB(x)      
cf2py y=ISUB2(x)
      
      real*8 ein,xin,yin,zin,                        
     *       uin,vin,win,wtin
      integer iqin,irin

      call showor(iqin,ein,xin,yin,zin,uin,vin,win,irin,wtin,ISUB,ISUB2)
      
      END

!*********************************************************************
!      ADD A NEW MEDIA TO MEDIA.MEDIA
!*********************************************************************
!23456789|123456789|123456789|123456789|123456789|123456789|123456789|12

      subroutine addmed(medarr,ch)

      include 'include/egs5_h.f'                

      include 'include/egs5_bounds.f'
      include 'include/egs5_media.f'
      include 'include/egs5_misc.f'
      include 'include/egs5_thresh.f'
      include 'include/egs5_useful.f'
      include 'include/egs5_usersc.f'
      include 'include/randomm.f'
                          
      character*(*) medarr
      integer j
      real    ch
      integer i,length
      
      nmed=nmed+1
      j=nmed

      length=LEN(medarr)
      if(length>24) then
        print *, '[ warning ] The length of the media name'
        print *, '    is longer than 24, only the first 24'
        print *, '    used. But this may lead to problem. '
      endif
      do i=1,MIN(24,length)
          media(i,j)=' '          
          media(i,j)=medarr(i:i)
      end do
      do i=MIN(24,length)+1,24
          media(i,j)=' '          
      end do
! nmed and dunit default to 1, i.e. one medium and we work in cm

      chard(j) = ch
      
      end          


!*********************************************************************
!      LIST MEDIA.MEDIA
!*********************************************************************
!23456789|123456789|123456789|123456789|123456789|123456789|123456789|12
      subroutine lsmed

      include 'include/egs5_h.f'                

      include 'include/egs5_bounds.f'
      include 'include/egs5_media.f'
      include 'include/egs5_misc.f'
      include 'include/egs5_thresh.f'
      include 'include/egs5_useful.f'
      include 'include/egs5_usersc.f'
      include 'include/randomm.f'
                          
      character*(24) medarr
      integer j                                    

      print *, ' NMED = ',nmed
      print *, '-----------'
      

100   format(I3, ' med = ',A24,' ap = ',f8.4,' up = ',f8.4, 
     *                         ' ae = ',f8.4,' ue = ',f8.4,
     *                         ' chard = ',f8.4)
      do j=1,nmed
          do i=1,24
          medarr(i:i)=media(i,j)
          end do
          
          write(*,100) j,medarr,ap(j),up(j),ae(j),ue(j),chard(j) 
      end do  
      
      
      end          
      
!*********************************************************************
!      RESET MEDIA.MEDIA
!*********************************************************************
!23456789|123456789|123456789|123456789|123456789|123456789|123456789|12
      subroutine rstmed

      include 'include/egs5_h.f'                

      include 'include/egs5_bounds.f'
      include 'include/egs5_media.f'
      include 'include/egs5_misc.f'
      include 'include/egs5_thresh.f'
      include 'include/egs5_useful.f'
      include 'include/egs5_usersc.f'
      include 'include/randomm.f'

      character*(24) medarr
      integer j                                    

      do j=1,nmed
          do i=1,24
          media(i,j)=' '
          end do
          nmed=nmed-1
      end do  
      
      end          
      

!*********************************************************************
!      Index MEDIA.MEDIA
!*********************************************************************
!23456789|123456789|123456789|123456789|123456789|123456789|123456789|12
      integer function fndmed(medarr)

      include 'include/egs5_h.f'                

      include 'include/egs5_bounds.f'
      include 'include/egs5_media.f'
      include 'include/egs5_misc.f'
      include 'include/egs5_thresh.f'
      include 'include/egs5_useful.f'
      include 'include/egs5_usersc.f'
      include 'include/randomm.f'

      character*(24) medarr
      integer i,j,k,k2,length                                    

      length=LEN(medarr)
      if(length>24) then
        print *, '[ warning ] The length of the media name'
        print *, '    is longer than 24, only the first 24'
        print *, '    used. But this may lead to problem. '
      endif

      do j=1,nmed
          k=1
          do i=1,MIN(24,length)
          if(media(i,j).EQ.medarr(i:i)) then
              k2=1
          else
              k2=0
          end if         
          k=k*k2
          end do
          do i=MIN(24,length)+1,24
          if(media(i,j).EQ.' ') then
              k2=1
          else
              k2=0
          end if         
          k=k*k2         
          end do 
                    
          if (k>0) then
              fndmed=j
              return
          end if
      end do  
      
      fndmed=-99999
      return
      
      end          


      